package cc.chinagps.gateway.unit.eg.upload.bean;

import cc.chinagps.gateway.unit.eg.util.EGTimeFormatUtil;
import lombok.Data;
import org.seg.lib.util.Util;

import java.util.ArrayList;
import java.util.List;

@Data
public class JMCFault {
    long faultTime;
    int dtcType;
    List<JMCEcuInfo> ecuInfoList;

    public void parse(byte[] data, int pos) throws Exception {
        String collectTime = "20" + cc.chinagps.gateway.util.Util.getDateTime(data, pos, 6);
        faultTime = EGTimeFormatUtil.parseGMT8(collectTime).getTime();
        pos += 6;

        int serialNo = Util.getShort(data, pos);
        pos += 2;

        dtcType = data[pos++];
        int ecuNum = data[pos++];
        if (ecuNum > 0) {
            ecuInfoList = new ArrayList<>();
            for (int i = 0; i < ecuNum; i++) {
                JMCEcuInfo jmcEcuInfo = new JMCEcuInfo();
                int ecuCode = data[pos++];
                jmcEcuInfo.setEcuCode(ecuCode);
                int dtcInfoNum = Util.getShort(data, pos);
                pos += 2;
                List<JMCDTCInfo> jmcdtcInfos = null;
                if (dtcInfoNum > 0 && dtcType == 2) {
                    jmcdtcInfos = new ArrayList<>();
                    for (int j = 0; j < dtcInfoNum; j++) {
                        JMCDTCInfo jmcdtcInfo = new JMCDTCInfo();
                        int dtcCode = Util.getShort(data, pos) & 0xFFFF;
                        pos += 2;
                        int failureType = data[pos++] & 0xFF;
                        int statusCover = data[pos++] & 0xFF;
                        jmcdtcInfo.setDtcCode(dtcCode);
                        jmcdtcInfo.setFailureType(failureType);
                        jmcdtcInfo.setStatusCover(statusCover);
                        jmcdtcInfos.add(jmcdtcInfo);
                    }
                }
                jmcEcuInfo.setJmcDTCInfos(jmcdtcInfos);
                ecuInfoList.add(jmcEcuInfo);
            }
        }
    }
}
